chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === 'calculateWorkdays') {
    const startDate = new Date(request.date);
    const endDate = new Date();

    // 返回 true 表示需要保持消息端口打开
    calculateWorkdays(startDate, endDate).then(workdays => {
      sendResponse({ workdays: workdays, workhours: workdays * 8 });
    }).catch(error => {
      console.error('Error calculating workdays:', error);
      sendResponse({ error: 'Failed to calculate workdays' });
    });

    // 告诉 Chrome 消息端口需要保持打开状态
    return true;
  }
});
async function calculateWorkdays(startDate, endDate) {
  let workdays = 0;
  const currentDate = new Date(startDate);
  const holidays = await getCachedHolidays();

  while (currentDate <= endDate) { // 修改条件以包含结束日期
    const dateString = currentDate.toISOString().split('T')[0];
    const isHoliday = holidays[dateString];

    if (!isHoliday) {
      workdays++;
    }
    currentDate.setDate(currentDate.getDate() + 1);
  }
  return workdays;
}

async function getCachedHolidays() {
  return new Promise((resolve, reject) => {
    fetch(chrome.runtime.getURL('holidays.json'))
      .then(response => response.json())
      .then(data => resolve(data))
      .catch(error => reject(error));
  });
}

async function cacheHoliday(dateString, isHoliday) {
}

async function fetchAndCacheHolidays(startDate, endDate) {
}